package com.wiloon.androidx.list;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.wiloon.androidx.R;
import com.wiloon.androidx.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by wangyue on 8/12/2014.
 */
public class ListViewActivity extends BaseActivity {
    private ListView listView;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.list_activity);
        listView = (ListView) findViewById(R.id.listViewFoo);
        ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 20; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemTitle", "Item " + i);
            map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");
            listItems.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this, listItems, R.layout.list_content,
                new String[]{"ItemTitle", "ItemText"},
                new int[]{R.id.listContentTextTitle, R.id.listContentText});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setTitle("item " + position + " clicked");
            }
        });

        listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("Long Click");
                menu.add(0, 0, 0, "Menu Item 0");
                menu.add(0, 1, 1, "Menu Item 1");
            }
        });
    }

    @Override
    public boolean onContextItemSelected(MenuItem menuItem) {
        setTitle("Menu Item " + menuItem.getItemId() + " Clicked.");
        return super.onContextItemSelected(menuItem);
    }
}
